package mi.payment.admin.pay88;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import mi.payment.R;

public class IPay88WalletDialog extends Dialog {

    private ImageView mIvIcon;
    private TextView mTvName;
    private CheckBox mCbMerchant;
    private EditText mEditMerchant;
    private CheckBox mCbUser;
    private EditText mEditUser;

    private Context context;
    private IPay88Wallet wallet;

    public IPay88WalletDialog(@NonNull Context context, IPay88Wallet wallet) {
        super(context, R.style.DefaultDialogStyle);
        this.wallet = wallet;
        this.context = context;
        init();
        initData();
    }

    private void init() {
        setContentView(R.layout.admin_pay88_dialog_wallet);
        Window window = getWindow();
        if (window != null) {
            window.getAttributes().gravity = Gravity.CENTER;
        }
        setCancelable(false);
        setCanceledOnTouchOutside(true);
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            setOwnerActivity(activity);
        }
        mIvIcon = findViewById(R.id.iv_icon);
        mTvName = findViewById(R.id.tv_name);
        mCbMerchant = findViewById(R.id.cb_merchant);
        mEditMerchant = findViewById(R.id.edit_merchant);
        mCbUser = findViewById(R.id.cb_user);
        mEditUser = findViewById(R.id.edit_user);
        findViewById(R.id.mb_ok).setOnClickListener(
                v -> {
                    wallet.merchantSwitch = mCbMerchant.isChecked();
                    wallet.merchantPaymentID = mEditMerchant.getText().toString();
                    wallet.userSwitch = mCbUser.isChecked();
                    wallet.userPaymentID = mEditUser.getText().toString();
                    IPay88Operation.getInstance().saveIPay88Wallet(wallet);
                    dismiss();
                }
        );
    }

    private void initData() {
        mTvName.setText(wallet.name);
        mIvIcon.setImageResource(wallet.iconResId);
        mCbMerchant.setChecked(wallet.merchantSwitch);
        boolean equal = TextUtils.isEmpty(wallet.merchantPaymentID) || TextUtils.equals("000", wallet.merchantPaymentID);
        if (equal) {
            mEditMerchant.setText("");
        } else {
            mEditMerchant.setText(wallet.merchantPaymentID);
        }
        mCbUser.setChecked(wallet.userSwitch);
        equal = TextUtils.isEmpty(wallet.userPaymentID) || TextUtils.equals("000", wallet.userPaymentID);
        if (equal) {
            mEditUser.setText("");
        } else {
            mEditUser.setText(wallet.userPaymentID);
        }
    }


}
